﻿namespace Pub.Class
{
    using System;
    using System.Collections.Generic;

    public class Cache2
    {
        private static readonly string PubClassCache = (WebConfig.GetApp("PubClassCache") ?? "WebCache");
        private static readonly ICache2 _cache = ((ICache2) "Pub.Class.{0},Pub.Class.{0}".FormatWith(PubClassCache).LoadClass());

        public static void Clear()
        {
            _cache.Clear();
        }

        public static void Compress<T>(string key, T obj) where T: class
        {
            _cache.Compress<T>(key, obj);
        }

        public static void Compress<T>(string key, T obj, int seconds) where T: class
        {
            _cache.Compress<T>(key, obj, seconds);
        }

        public static bool ContainsKey(string key)
        {
            return _cache.ContainsKey(key);
        }

        public static T Decompress<T>(string key) where T: class
        {
            return _cache.Decompress<T>(key);
        }

        public static object Get(string key)
        {
            return _cache.Get(key);
        }

        public static T Get<T>(string key)
        {
            if (!ContainsKey(key))
            {
                return default(T);
            }
            return (T) _cache.Get(key);
        }

        public static T Get<T>(string key, Func<T> acquire)
        {
            return Get<T>(key, 1, acquire);
        }

        public static T Get<T>(string key, int cacheTime, Func<T> acquire)
        {
            if (ContainsKey(key))
            {
                return (T) Get(key);
            }
            T local = acquire();
            _cache.Insert(key, local, cacheTime);
            return local;
        }

        public static IList<CachedItem> GetList()
        {
            return _cache.GetList();
        }

        public static void Insert(string key, object obj)
        {
            _cache.Insert(key, obj);
        }

        public static void Insert(string key, object obj, int seconds)
        {
            _cache.Insert(key, obj, seconds);
        }

        public static void Remove(string key)
        {
            _cache.Remove(key);
        }

        public static void RemoveByPattern(string pattern)
        {
            _cache.RemoveByPattern(pattern);
        }
    }
}

